<?php

/**
 * 知识点
 * Class KnowledgeController
 */
class KnowledgeController extends BaseController
{
    use \app\library\traits\Restful;

    /**
     * 根据分类获取列表
     */
    public function getListAction()
    {
        $category_id = intval($this->getRequest()->getQuery('category_id'));
        $page = intval($this->getRequest()->getQuery('page'));
        $pagesize = intval($this->getRequest()->getQuery('pagesize'));

        if (empty($category_id) || empty($page) || empty($pagesize)) {
            return $this->returnError(10001, '缺少参数');
        }
        $list = (new KnowledgeModel())->findListBycategory($category_id, $page, $pagesize);
        if (!$list) {
            return $this->returnError(10006, '不存在');
        }

        return $this->returnSuccess([
            'list' => $list
        ]);
    }

    /**
     * 获取详情
     */
    public function read(){
        $id = intval($this->getRequest()->getQuery('id'));
        if (empty($id)) {
            return $this->returnError(10001, '缺少参数');
        }
        $list = (new KnowledgeModel())->find($id);
        if (!$list) {
            return $this->returnError(10006, '不存在');
        }
        if ($list['status'] == KnowledgeModel::status['close']) {
            return $this->returnError(10005, '已下架');
        }
        return $this->returnSuccess($list);
    }

    /**
     * 获取全部可用分类
     */
    public function getCategoryAllAction(){
        return $this->returnSuccess((new KnowledgeCategoryModel())->findAllOpen());
    }

    /**
     * 获取最新的几条记录
     */
    public function getNewListAction()
    {
        $list = (new KnowledgeModel())->findNewList(6);
        return $this->returnSuccess($list);
    }
}
